<%@ page import="org.codehaus.groovy.grails.plugins.springsecurity.AuthorizeTools" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="layout" content="main"/>
    <title><g:message code="job.nav.edit"/></title>
</head>
<body>
<div class="nav">
    <span class="menuButton"><g:link class="list" action="list"><g:message code="job.nav.list"/></g:link></span>
    <span class="menuButton"><g:link class="create" action="create" params="['customer.id':jobInstance?.customer?.id]"><g:message code="job.nav.create"/></g:link></span>
</div>
<div class="body">
    <h1><g:message code="job.nav.edit"/></h1>
    <g:if test="${flash.message}">
        <div class="message">${flash.message}</div>
    </g:if>
    <g:hasErrors bean="${jobInstance}">
        <div class="errors">
            <g:renderErrors bean="${jobInstance}" as="list"/>
        </div>
    </g:hasErrors>
    <g:javascript library="prototype"/>
    <script type="text/javascript">
        function submitFormWithProcessAction() {
            var jobForm = $('jobForm');
            jobForm.action = '/PhotoShop/job/process';
            jobForm.submit();
        }

        function checkActualValue() {
            var status = '${jobInstance?.status}';
            if (status == '${Constants.JOB_STATUS_PENDING}' || status == '${Constants.JOB_STATUS_DONE}'){
                submitFormWithProcessAction();
                return;
            }

            var actualValue = prompt('${message(code:'job.promt.enter.actual.value')}', $('value').value);
            if (actualValue) {
                $('actualValue').value = actualValue;
                submitFormWithProcessAction();
            }
        }
    </script>
    <g:form name="jobForm" method="post">
        <input type="hidden" name="id" value="${jobInstance?.id}"/>
        <div class="dialog">
            <table>
                <tbody>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="actualValue"><g:message code="job.table.actual.value"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'actualValue', 'errors')}">
                        <input type="text" id="actualValue" name="actualValue" value="${fieldValue(bean: jobInstance, field: 'actualValue')}"/>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="customer"><g:message code="job.table.customer"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'customer', 'errors')}">
                        <g:if test="${(jobInstance?.status == Constants.JOB_STATUS_CREATED || AuthorizeTools.ifAnyGranted('ROLE_ADMIN'))}">
                            <g:select optionKey="id" from="${Customer.list()}" name="customer.id" value="${jobInstance?.customer?.id}"/>
                        </g:if>
                        <g:else>
                            <input type="hidden" name="customer.id" value="${jobInstance?.customer?.id}"/>
                            ${jobInstance?.customer}
                        </g:else>

                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="date"><g:message code="job.table.date"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'date', 'errors')}">
                        %{--<g:datePicker name="date" value="${jobInstance?.date}"></g:datePicker>--}%
                        ${jobInstance?.date}
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="discount"><g:message code="job.table.discount"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'discount', 'errors')}">
                        <input type="text" id="discount" name="discount" value="${fieldValue(bean: jobInstance, field: 'discount')}"/>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="jobItems"><g:message code="job.table.jobitems"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'jobItems', 'errors')}">

                        <ul>
                            <g:each var="j" in="${jobInstance?.jobItems}">
                                <li><g:link controller="jobItem" action="show" id="${j.id}">${j?.encodeAsHTML()}</g:link></li>
                            </g:each>
                        </ul>
                        <g:if test="${(jobInstance?.status == Constants.JOB_STATUS_CREATED || AuthorizeTools.ifAnyGranted('ROLE_ADMIN'))}">
                            <g:link controller="jobItem" params="['job.id':jobInstance?.id]" action="create"><g:message code="job.link.add.jobitem"/></g:link>
                        </g:if>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="status"><g:message code="job.table.status"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'status', 'errors')}">
                        <g:ifAnyGranted role="ROLE_ADMIN">
                            <g:select name="status" value="${jobInstance.status}" from="${Constants.JOB_STATUS_LIST}"/>
                        </g:ifAnyGranted>
                        <g:ifNotGranted role="ROLE_ADMIN">
                            <input type="text" class="readonly" readonly name="status" value="${fieldValue(bean: jobInstance, field: 'status')}"/>
                        </g:ifNotGranted>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="originalValue"><g:message code="job.table.original.value"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'originalValue', 'errors')}">
                        <input type="text" id="originalValue" name="originalValue" value="${fieldValue(bean: jobInstance, field: 'originalValue')}" readonly/>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="value"><g:message code="job.table.value"/>:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: jobInstance, field: 'value', 'errors')}">
                        <input type="text" id="value" name="value" value="${fieldValue(bean: jobInstance, field: 'value')}" readonly/>
                    </td>
                </tr>

                </tbody>
            </table>
        </div>
        <div class="buttons">
            <span class="button"><g:actionSubmit class="edit" value="${message(code: 'common.button.edit')}" action="update"/></span>
            <span class="button"><g:render template="/shared/processButton" model="[currentStatus:jobInstance.status, onclick:'checkActualValue()']"/></span>
            <span class="button"><g:actionSubmit class="delete" onclick="return confirm('${message(code:'common.delete.confirm')}');" value="Delete"/></span>
        </div>
    </g:form>
</div>
</body>
</html>
